Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1g  None of the above.  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native, strictfp, or synchronized so the same restriction applies to methods declared within an interface. Transient and volatile are not method modifiers.  
2a  static  All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. A field that is declared final can not also be declared volatile so a field of an interface can not be declared volatile. The synchronized and strictfp modifiers are never applicable to a field.  
3e  strictfp  The private, protected, and static modifiers are applicable to member interfaces only. The synchronized modifier is applicable only to concrete implementations of methods. The transient and volatile modifiers are applicable only to variables that are members of a class. The strictfp modifier causes all float and double expressions within the interface declaration to be FP-strict. Please note that the abstract methods declared within the interface are never strictfp.  
4c  d  3  4  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public a weaker access level can not be declared.  
5c  d  e  3  4  5  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public a weaker access level can not be declared.  
6a  Prints: ABCI  The field access expression ((X)this).identifier can be used to access hidden fields that are visible within the super class or interface X.  
7h  Compiler error at line 13.  Class C10 inherits ambiguous declarations of the "name" field. As long as the field is not referenced as a member of class C10, then no compiler error occurs. Line 13 generates the compiler error, because it is the first to access the "name" field as a member of class C10.  
8f  Compiler error.  Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, no method is clearly more specific than all of the others.  
9c  Prints: m3Cm2Bm1A  Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, the most specific version of method m3 is the one that declares a parameter of type C. The most specific version of m2 is the one that declares a parameter of type B.  
10a  Prints: M.printS1,M.s1 N.printS1,N.s1  A static method may hide a method in the super class, but a static method does not override a super class method.  
11b  Prints: B  Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, type B is more specific than type A because B extends A.  
12b  Prints: DE  The instance variable referred to by x.s1 depends on the type of the reference at compile time. The instance method referred to by x.getS1() depends on the type of the object at runtime.  
13f  Prints: H.printS1,null  The default constructor of H calls the constructor of G. The constructor of G calls the overridden method printS1 causing the invocation of H.printS1. The instance variable H.s1 hides G.s1. Although G.s1 has already been initialized, the initialization of the instance variables of H won't begin until the initialization and construction of G is complete.  
14f  Compiler error  An instance method can not override a static method.  
15b  Prints: DDDD  Instance method D.m1 overrides the superclass methods and is therefore always invoked in place of the superclass methods even if the reference is cast to the superclass type. The instance method that is invoked depends on the type of the object and not the type of the reference.  
16e  System.out.print(new A.B().m1());  A class nested within an enclosing interface can legally implement the enclosing interface. It is not possible to create an instance of an interface so the syntax new A().new B().m1() is not applicable. A class declared within an interface is implicitly static so an instance of the nested class can be created without first making a futile attempt to create an instance of the enclosing interface. The fully qualified name of class B is A.B so an instance of A.B can be created using the expression new A.B().  
17b  Prints: D,C,B,A  Inherited fields hide super class fields but do not override super class fields. The field that is accessed at run time depends on the type of the reference.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1